﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SuperScrollView;
using UnityEngine;
using UnityEngine.UI;

public class HappyCtrl : MonoBehaviour
{
    public LoopListView2 mLoopListView;
    public Button jieguoBtn;
    public Button shaBtn;
    public Button resetBtn;

    public Button loadBtn;
    public Button saveBtn;

    public InputField input;
    public Text tips;

    #region 数据

    public List<int[]> envCurData;

    #endregion

    public bool isNeedReload = false;
    private Thread thread;

    public void OnChangeMode()
    {
        envCurData = reloadShuangseQiu();

        mLoopListView.InitListView(envCurData.Count, OnGetItemByIndex);
    }

    public void SaveData()
    {
        using (var stream = File.AppendText($"./ball_history.txt"))
        {
            for (int i = 0; i < envCurData.Count; i++)
            {
                stream.Write(
                    $"{envCurData[i][0]},{envCurData[i][1]},{envCurData[i][2]},{envCurData[i][3]},{envCurData[i][4]},{envCurData[i][5]}\n");
            }
        }
    }

    public void LoadData()
    {
        envCurData.Clear();
        envCurData = new List<int[]>();
        var allLines = File.ReadAllLines("./ball_history.txt");
        for (int i = 0; i < allLines.Length; i++)
        {
            string line = allLines[i];
            var result = line.Split(',');
            List<int> rest_int = new List<int>();
            for (int j = 0; j < result.Length; j++)
            {
                rest_int.Add(int.Parse(result[i]));
            }

            envCurData.Add(rest_int.ToArray());
        }

        Deleted();
    }

    public void ResetAddListen()
    {
        resetBtn.onClick.AddListener(() =>
        {
            envCurData = reloadShuangseQiu();
            Deleted();
        });

        saveBtn.onClick.RemoveAllListeners();
        saveBtn.onClick.AddListener(() =>
        {
            Thread th = new Thread(new ThreadStart(SaveData));
            th.Start();
        });


        loadBtn.onClick.RemoveAllListeners();

        loadBtn.onClick.AddListener(() => { LoadData(); });

        shaBtn.onClick.AddListener(() =>
        {
            thread = new Thread(new ThreadStart(ShaShuangseQiu));
            thread.Start();
        });

        jieguoBtn.onClick.AddListener(() =>
        {
            LoopInstance.Instance.NewPanel("statisticalPanel").GetComponent<statisticalList>()
                .SetData(envCurData, ModeType.ShuangSeQiu);
        });
    }

    public void ShowTips(string txt)
    {
        TipsText = txt;
    }


    public void Deleted()
    {
        mLoopListView.SetListItemCount(envCurData.Count, false);
        mLoopListView.RefreshAllShownItem();
        ShowTips("剩余 ： " + envCurData.Count.ToString());
    }

    LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index)
    {
        if (index < 0 || index >= envCurData.Count)
        {
            return null;
        }

        LoopListViewItem2 item = listView.NewListViewItem("item");
        LoopTwoBall itemScript = item.GetComponent<LoopTwoBall>();
        itemScript.SetData(envCurData[index]);
        itemScript.OnInit();
        return item;
    }

    public List<int[]> reloadShuangseQiu()
    {
        List<int[]> datas = new List<int[]>();
        int max = 33;
        for (int i = 1; i <= max - 5; i++)
        {
            for (int i2 = i + 1; i2 <= max - 4; i2++)
            {
                for (int i3 = i2 + 1; i3 <= max - 3; i3++)
                {
                    for (int i4 = i3 + 1; i4 <= max - 2; i4++)
                    {
                        for (int i5 = i4 + 1; i5 <= max - 1; i5++)
                        {
                            for (int i6 = i5 + 1; i6 <= max; i6++)
                            {
                                datas.Add(new int[6] {i, i2, i3, i4, i5, i6});
//                                stream.WriteLine($"{i} , {i2} , {i3} , {i4} , {i5} , {i6}");
                            }
                        }
                    }
                }
            }
        }

        return datas;
    }


//    IEnumerator StartShuangSeQiu()
//    {
//        
//    }
    public string TipsText = "";

    public void ShaShuangseQiu()
    {
        if (input.text.Equals(""))
        {
            ShowTips("输入大复试，以英文,分割 例如 1,2,3,4,5");
            return;
        }

        var content = input.text;
        content = content.Replace('，', ',');
        content = content.Replace('.', ',');
//        Debug.Log("杀号" + content);
        var sp_content = content.Split(',');
        List<int> input_nums = new List<int>();
        for (int i = 0; i < sp_content.Length; i++)
        {
            var ngnum = sp_content[i];
            int ret = -1;
            if (!int.TryParse(ngnum, out ret))
            {
                ShowTips("输入不合法 ,请检查一下");
                return;
            }

            input_nums.Add(ret);
        }

        ShowTips("杀号中...");
        int count = input_nums.Count;
        List<int[]> datas = new List<int[]>();
        int max = count;

        if (count == 6)
        {
            datas.Add(input_nums.ToArray());
        }
        else
        {
            for (int i = 1; i <= max - 5; i++)
            {
                for (int i2 = i + 1; i2 <= max - 4; i2++)
                {
                    for (int i3 = i2 + 1; i3 <= max - 3; i3++)
                    {
                        for (int i4 = i3 + 1; i4 <= max - 2; i4++)
                        {
                            for (int i5 = i4 + 1; i5 <= max - 1; i5++)
                            {
                                for (int i6 = i5 + 1; i6 <= max; i6++)
                                {
                                    datas.Add(new int[6]
                                    {
                                        input_nums[i - 1], input_nums[i2 - 1], input_nums[i3 - 1], input_nums[i4 - 1],
                                        input_nums[i5 - 1],
                                        input_nums[i6 - 1]
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }


        ShowTips($"输入数的数量 {input_nums.Count}  组合数量{datas.Count}");
        List<int> disIndexs = new List<int>();


        for (int i = 0; i < datas.Count; i++)
        {
            var data_temp = datas[i];
            ShowTips($"当前数的数量{data_temp.Length}");

            {
                var index = envCurData.FindIndex((elel) =>
                {
                    return elel[0] == data_temp[0] && elel[1] == data_temp[1] && elel[2] == data_temp[2] &&
                           elel[3] == data_temp[3] && elel[4] == data_temp[4] && elel[5] == data_temp[5];
                });

                if (index >= 0)
                {
                    disIndexs.Add(index);
                }

                disIndexs.Sort();

                if (disIndexs.Count == 0)
                {
                    return;
                }

                ShowTips($"杀掉 {envCurData.Count} {disIndexs.Count}");
                for (int max_i = disIndexs.Count - 1; max_i >= 0; max_i--)
                {
                    envCurData.RemoveAt(disIndexs[max_i]);
                }
            }
        }

        isNeedReload = true;
        ShowTips("杀号完成");

//        LoopInstance.Instance.NewPanel("ShaHaoPanel").GetComponent<LoopCtrl>().SetData(datas);
    }

    private void Update()
    {
        if (isNeedReload == true)
        {
            Deleted();
            isNeedReload = false;
        }

        if (TipsText != string.Empty && TipsText != "")
        {
            tips.text = TipsText;
            TipsText = string.Empty;
        }
    }

    private void OnDestroy()
    {
        thread.Abort();
    }
}